﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
using System.Xml.Linq;

namespace RaisingStudio.SmallProgram.Library.Internal
{
    internal static class RestHelper
    {
        internal static XDocument GetContents(string url)
        {
#if XBOX
            // TODO:
            return null;
#else
            try
            {
                System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
                ManualResetEvent manualResetEvent = new ManualResetEvent(false);
                IAsyncResult asyncResult = webRequest.BeginGetResponse(delegate(IAsyncResult o)
                {
                    manualResetEvent.Set();
                }, null);
                manualResetEvent.WaitOne();
                using (System.Net.WebResponse WebResponse = webRequest.EndGetResponse(asyncResult))
                {
                    using (Stream responseStream = WebResponse.GetResponseStream())
                    {
                        return XDocument.Parse(new StreamReader(responseStream).ReadToEnd());
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            return null;
#endif
        }
    }
}

